//
//  LoveViewController.m
//  XiangMuB
//
//  Created by lanou3g on 16/8/23.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "LoveViewController.h"
#import "textSSViewController.h"
#import "voicessViewController.h"
#import "loadShare.h"
#import "NSObject+alertView.h"
@interface LoveViewController ()<UIScrollViewDelegate>

@property (strong,nonatomic)UIButton *button;
@property (strong,nonatomic)UIScrollView *scrollview;
@property (strong,nonatomic)UIPageControl *pagecontrol;
@property (strong,nonatomic)UITextView *textView;
@end

@implementation LoveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
   //创建轮播视图
    
    self.scrollview =[[UIScrollView alloc] initWithFrame:CGRectMake(0, 65, self.view.frame.size.width, 200)];
    self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width *4, 0);
    self.scrollview.backgroundColor =[UIColor whiteColor];
    self.scrollview.bounces = NO;
    self.scrollview.pagingEnabled = YES;
    self.scrollview.showsHorizontalScrollIndicator = NO;
    self.scrollview.showsVerticalScrollIndicator = NO;
    
    for (int i = 0 ; i < 4; i++) {
        
        UIImageView *image =[[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width *i ,-65, self.view.frame.size.width, 200)];
        image.backgroundColor = [UIColor redColor];
        image.image = [UIImage imageNamed:[NSString stringWithFormat:@"a%d.jpg",i]];

        
        [self.scrollview addSubview:image];
    }
    //创建代理
    self.scrollview.delegate = self;

    [self.view addSubview:self.scrollview];
    
    
    self.pagecontrol =[[UIPageControl alloc] initWithFrame:CGRectMake(0, 245, self.view.frame.size.width, 20)];
    self.pagecontrol.numberOfPages = 4;
    self.pagecontrol.currentPageIndicatorTintColor = [UIColor lightGrayColor];
    self.pagecontrol.pageIndicatorTintColor = [UIColor blackColor];
    self.pagecontrol.backgroundColor = [UIColor clearColor];
    [self.pagecontrol addTarget:self action:@selector(pageAction:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:_pagecontrol];
    
    
    UIImageView *view =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"北京.jpg"]];
    view.frame = CGRectMake(0, 265, self.view.frame.size.width, self.view.frame.size.height-256);
    [self.view addSubview:view];
    

    //定时器
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    
    //分页控制器
    UISegmentedControl *seg =[[UISegmentedControl alloc]initWithItems:@[@"我发送的消息",@"我接受的消息"]];
    seg.backgroundColor = [UIColor orangeColor];
    seg.frame = CGRectMake(0, 280, self.view.frame.size.width, 40);
    [seg setTintColor: [UIColor whiteColor]];
    
    [seg addTarget:self action:@selector(segAction:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:seg];
    
    
    UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(20, 340, 300, 40)];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:21];
    label.text = @"每日吐槽";
    [label setTextColor:[UIColor orangeColor]];
    [self.view addSubview:label];
    
    
    
    
    //获取沙盒路径
    NSString *document =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)firstObject];
    
    document = [document stringByAppendingFormat:@"%@.text",[loadShare shareLoad].userName];
    
    NSData *newdata =[NSData dataWithContentsOfFile:document];
  
    NSString *str =[[NSString alloc] initWithData:newdata encoding:4];
    
   _textView =[[UITextView alloc] initWithFrame:CGRectMake(20, 400, self.view.frame.size.width -40, 200)];
    _textView.text = str;
    _textView.userInteractionEnabled = NO;
    _textView.backgroundColor = [UIColor whiteColor];
    [_textView setFont:[UIFont systemFontOfSize:17]];
    [_textView setTextColor:[UIColor orangeColor]];

    [self.view addSubview:_textView];
    

   _button = [UIButton buttonWithType:(UIButtonTypeCustom)];
 
    _button.backgroundColor = [UIColor orangeColor];
   
    _button.frame= CGRectMake(340, 360, 40, 30);
    [_button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
     [_button setTitle:@"编辑" forState:(UIControlStateNormal)];
    [_button setTag:3];
 
    [self.view addSubview:_button];
    
    
    
    
    
}
//button点击事件
-(void)buttonAction:(UIButton *)sender
{
    if ([loadShare shareLoad].load == YES) {
    if (sender.tag ==2) {
        
        
        
        
            //获取沙盒路径
            NSString *document =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)firstObject];
            
            document = [document stringByAppendingFormat:@"%@.text",[loadShare shareLoad].userName];
            NSData *data =[_textView.text dataUsingEncoding:4];
            
            BOOL  result= [data writeToFile:document atomically:YES];
            
            
            if (result) {
                NSLog(@"恭喜,写入成功%@",document);
            }else
            {
                NSLog(@"嘻嘻,失败了");
            }
        

                _textView.userInteractionEnabled = NO;
         [_button setTitle:@"编辑" forState:(UIControlStateNormal)];
    
        
        sender.tag = 3;
    }else if (sender.tag == 3){
        
               [_button setTitle:@"保存" forState:(UIControlStateNormal)];
          _textView.userInteractionEnabled = YES;
          sender.tag = 2;
        
    }
    
    }else
    {
        [NSObject alterString:@"功能受限,请登录!"];
    }

    
}
//分页控制器实现
-(void)segAction:(UISegmentedControl *)sender
{
    switch (sender.selectedSegmentIndex) {
        case 0:{
            textSSViewController *text =[textSSViewController new];
            [self.navigationController pushViewController:text animated:YES];
        }
            break;
            case 1:
        {
            voicessViewController *voice =[voicessViewController new];
            [self.navigationController pushViewController:voice animated:YES];
        }
            break;
            
        default:
            break;
    }
}




//页面控制器
-(void)pageAction:(UIPageControl *)seder
{
    
    
    self.scrollview.contentOffset = CGPointMake(self.pagecontrol.currentPage*self.view.frame.size.width, -65);
}

-(void)timerAction:(NSTimer *)sender
{
    if (self.pagecontrol.currentPage ==3) {
        self.pagecontrol.currentPage =0 ;
    }else
    {
        self.pagecontrol.currentPage ++;
    }
    
    CGPoint point =CGPointMake(self.pagecontrol.currentPage *self.view.frame.size.width, -65);
    if (self.pagecontrol.currentPage == 0) {
        [self.scrollview setContentOffset:point animated:NO];
    }else
    {
        [self.scrollview setContentOffset:point animated:YES];
    }
    
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pagecontrol.currentPage= self.scrollview.contentOffset.x/self.view.frame.size.width;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
